package com.hong610.controller;

import com.hong610.controller.base.BaseController;
import com.hong610.quartz.entity.JobClassInfo;
import com.hong610.quartz.entity.JobInfo;
import com.hong610.response.Response;
import com.hong610.service.QuartzService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Hong on 2017/2/8.
 */
@Controller
public class IndexControlelr extends BaseController {

    @Resource(name = "QuartzService")
    QuartzService quartzService;

    /**
     * 首页
     *
     * @return
     */
    @RequestMapping(value = "/", method = {RequestMethod.GET})
    public String index() {
        return "index";
    }

    /**
     * 欢迎页面
     *
     * @return
     */
    @RequestMapping(value = "/welcome.html", method = {RequestMethod.GET})
    public String welcome() {
        return "welcome";
    }

    /**
     * 任务类管理
     *
     * @return
     */
    @RequestMapping(value = "/jobClassManager.html", method = {RequestMethod.GET})
    public String jobClassManager() throws Exception {
        List<JobClassInfo> list = quartzService.getQuartzJobClass();
        map.put("list", list);
        return "job_class_manager";
    }

    /**
     * 调度任务管理
     *
     * @return
     */
    @RequestMapping(value = "/jobManager.html", method = {RequestMethod.GET})
    public String jobManager() throws Exception {
        List<JobInfo> list = quartzService.getJobAll();
        map.put("list", list);
        return "job_manager";
    }

    /**
     * 暂停
     *
     * @param triggerName  触发器
     * @param triggerGroup 工作组
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/pause.json", method = {RequestMethod.POST})
    public Response pause(String triggerName, String triggerGroup) throws Exception {
        boolean flag = quartzService.pause(triggerName, triggerGroup);
        if (!flag)
            return Response.Error();
        return Response.Success();
    }

    /**
     * 删除
     *
     * @param triggerName  触发器
     * @param triggerGroup 工作组
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/remove.json", method = {RequestMethod.POST})
    public Response remove(String triggerName, String triggerGroup) throws Exception {
        boolean flag = quartzService.remove(triggerName, triggerGroup);
        if (!flag)
            return Response.Error();
        return Response.Success();
    }

    /**
     * 重启
     *
     * @param triggerName  触发器
     * @param triggerGroup 工作组
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/restart.json", method = {RequestMethod.POST})
    public Response restart(String triggerName, String triggerGroup) throws Exception {
        boolean flag = quartzService.restart(triggerName, triggerGroup);
        if (!flag)
            return Response.Error();
        return Response.Success();
    }

    /**
     * 修改Quartz任务
     * @param triggerName  触发器
     * @param triggerGroup 工作组
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/edit.html", method = {RequestMethod.GET})
    public String edit(String triggerName, String triggerGroup) throws Exception {
        JobInfo jobInfo = quartzService.findOne(triggerName, triggerGroup);
        map.put("job", jobInfo);
        return "edit";
    }


    @ResponseBody
    @RequestMapping(value = "/edit.json", method = {RequestMethod.POST})
    public Response edit(JobInfo jobInfo, boolean isRestart) throws Exception {
        boolean flag = quartzService.edit(jobInfo);
        if (!flag)
            return Response.Error();
        return Response.Success();
    }
}
